package com.pig4cloud.pigx.contract.api.v1.controller;

import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.api.annotation.OpenApi;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.api.v1.service.OrderHistoryServiceV1;
import com.pig4cloud.pigx.contract.api.v1.service.OrderServiceV1;
import com.pig4cloud.pigx.contract.dto.ContractOrderCloseDTO;
import com.pig4cloud.pigx.contract.dto.ContractOrderCloseMarketDTO;
import com.pig4cloud.pigx.contract.dto.ContractOrderOpenDTO;
import com.pig4cloud.pigx.contract.entity.ContractOrder;
import com.pig4cloud.pigx.contract.open.api.dto.OpenApiOrderBookDTOV1;
import com.pig4cloud.pigx.contract.open.api.dto.OpenApiOrderDTOV1;
import com.pig4cloud.pigx.contract.open.api.vo.OpenApiOrderBookVOV1;
import com.pig4cloud.pigx.contract.open.api.vo.OpenApiOrderVOV1;
import com.pig4cloud.pigx.contract.vo.ContractOrderInitResVO;
import com.pig4cloud.pigx.user.enums.UserApiKeyEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 开放API - 订单 - v1版本
 *
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/contract/order")
@Api(value = "api_order_v1", tags = "开放api-订单")
public class OrderControllerV1 {

    private final OrderServiceV1 orderService;

    private final OrderHistoryServiceV1 orderHistoryService;

    /**
     * 获取用户当前全部挂单
     *
     * @return 挂单list
     */
    @ApiOperation(value = "获取用户当前全部挂单", notes = "获取用户当前全部挂单")
    @SysLog("获取用户当前全部挂单")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @GetMapping("/current")
    public R<List<OpenApiOrderVOV1>> getUserNowOrder() {
        return orderService.getUserNowOrder();
    }

    /**
     * 获取用户当前挂单 - 币对
     *
     * @param symbolName 币对名称
     * @return 挂单list
     */
    @ApiOperation(value = "获取用户当前挂单 - 币对", notes = "获取用户当前挂单 - 币对")
    @SysLog("获取用户当前挂单 - 币对")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @GetMapping("/current/symbol")
    public R<List<OpenApiOrderVOV1>> getUserNowOrder(@RequestParam("symbolName") String symbolName) {
        return orderService.getUserNowOrderBySymbol(symbolName);
    }

    /**
     * 查询订单详情
     *
     * @param condition 查询条件
     * @return {@link R< OpenApiOrderVOV1>}
     * @since 2023/1/9 19:19
     */
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    @SysLog("查询订单详情")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @GetMapping
    public R<OpenApiOrderVOV1> getOrder(ContractOrder condition) {
        OpenApiOrderVOV1 order = orderService.getOrder(condition);
        return R.ok(order);
    }

    /**
     * 根据订单id撤销订单
     *
     * @param orderId 订单id
     * @return r
     */
    @ApiOperation(value = "根据订单id撤销订单", notes = "根据订单id撤销订单")
    @SysLog("根据订单id撤销订单")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.CONTRACT, needSignature = true)
    @DeleteMapping
    public R cancel(@RequestParam("orderId") Long orderId) {
        return orderService.cancel(orderId);
    }

    /**
     * 撤销用户全部挂单
     *
     * @return r
     */
    @ApiOperation(value = "撤销用户全部挂单", notes = "撤销用户全部挂单")
    @SysLog("撤销用户全部挂单")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.CONTRACT, needSignature = true)
    @DeleteMapping("/all")
    public R cancelAll() {
        return orderService.cancelAll();
    }

    /**
     * 获取强平单历史
     *
     * @param dto dto
     * @return r
     */
    @ApiOperation(value = "获取强平单历史", notes = "获取强平单历史")
    @SysLog("获取强平单历史")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @PostMapping("/liquidate/history")
    public R<List<OpenApiOrderVOV1>> getLiquidateOrder(@RequestBody OpenApiOrderDTOV1 dto) {
        return orderHistoryService.getLiquidateOrder(dto);
    }

    /**
     * 获取用户历史订单
     *
     * @param dto dto
     * @return r
     */
    @ApiOperation(value = "获取用户历史订单", notes = "获取用户历史订单")
    @SysLog("获取用户历史订单")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @PostMapping("/history")
    public R<List<OpenApiOrderVOV1>> getAllOrderList(@RequestBody OpenApiOrderDTOV1 dto) {
        return orderHistoryService.getAllOrderList(dto);
    }

    /**
     * 开仓
     *
     * @param dto dto
     * @return {@link R< ContractOrderInitResVO>}
     */
    @ApiOperation(value = "开仓", notes = "开仓")
    @SysLog("开仓")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.CONTRACT, needSignature = true)
    @PostMapping("/open")
    public R<ContractOrderInitResVO> open(@RequestBody ContractOrderOpenDTO dto) {
        return orderService.open(dto);
    }

    /**
     * 平仓
     *
     * @param dto dto
     * @return {@link R< ContractOrderInitResVO>}
     */
    @ApiOperation(value = "平仓", notes = "平仓")
    @SysLog("平仓")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.CONTRACT, needSignature = true)
    @PostMapping("/close")
    public R<ContractOrderInitResVO> close(@RequestBody ContractOrderCloseDTO dto) {
        return orderService.close(dto);
    }

    /**
     * 市价全平
     *
     * @return {@link R}
     * @since 2022/8/16 18:07
     */
    @ApiOperation(value = "市价全平", notes = "市价全平")
    @SysLog("市价全平")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.CONTRACT, needSignature = true)
    @PostMapping("/close/market")
    public R<ContractOrderInitResVO> closeMarket(@Validated @RequestBody ContractOrderCloseMarketDTO dto) {
        return R.ok(orderService.closeMarket(dto));
    }

    /**
     * 获取最优订单
     *
     * @param dto dto
     * @return r
     */
    @ApiOperation(value = "获取最优订单", notes = "获取最优订单")
    @SysLog("获取最优订单")
    @OpenApi(version = "1", permission = UserApiKeyEnum.Permission.READ, needSignature = true)
    @PostMapping("/best")
    public R<OpenApiOrderBookVOV1> getOrderBook(@RequestBody OpenApiOrderBookDTOV1 dto) {
        return orderService.getOrderBook(dto);
    }

}
